<?php
/**
* @package reader
* @author Jan Papousek
* @copyright Jan Papousek 2007
* @link http://papi.chytry.cz
*/
/**
* Trida, ve ktere jsou ulozeny nektere veci pro praci s literarnimi soutezemi. 
* @package reader
*/
class Competition {
	
	/**
	 * Pocet soutezi, ktere se vrati jako naposledy vlozene.
	 */
	const LIMIT = 5;
	
	/**
	 * ID tematu, ve kterem jsou ulozene souteze v prvni kategorii.
	 */
	const ID_CATEGORY_1_TOPIC = 3;
	
	/**
	 * ID tematu, ve kterem jsou ulozene souteze v druhe kategorii.
	 */
	const ID_CATEGORY_2_TOPIC = 4;
	
	/**
	 * ID tematu, ve kterem jsou ulozene souteze ve treti kategorii.
	 */
	const ID_CATEGORY_3_TOPIC = 5;
	
	/**
	 * Vrati naposledy vlozene (komentovane souteze).
	 */
	public static function getLast() {
		$topics = self::getTopicsID();
		$set = "";
		foreach($topics AS $t) {
			if ($set != "") {
				$set .= ",";
			}
			$set .= $t;
		}
		$set = "($set)";
		$sql = "
			SELECT
				id,
				title,
				topic AS topicID,
				(SELECT MAX(help.date) FROM ".Discussion::getTableName()." AS help WHERE help.id = ".Discussion::getTableName().".id OR help.follow = ".Discussion::getTableName().".id) AS lastDate,
				(SELECT COUNT(help.id) FROM ".Discussion::getTableName()." AS help WHERE help.id = ".Discussion::getTableName().".id OR help.follow = ".Discussion::getTableName().".id) -1 AS numDis
			FROM ".Discussion::getTableName()."
			WHERE topic IN $set 
			ORDER BY lastDate DESC
			LIMIT 0,".self::LIMIT."
		";

		return MySQL::query($sql,__FILE__,__LINE__);
	}
	
	/**
	 * Vrati pole temat, ve kterych jsou ulozeny literarni souteze ve formatu (Nazev => ID).
	 * @return mixed
	 */
	public static function getTopics() {
		$topicID = self::getTopicsID();
		$cond = "";
		foreach($topicID AS $id) {
			if ($cond != "") {
				$cond .= ",";
			}
			$cond .= $id;
		}
		$cond = "($cond)";
		$sql = "SELECT id,name FROM ".Topic::getTableName()." WHERE id IN $cond";
		$res = MySQL::query($sql,__FILE__,__LINE__);
		$result = array();
		while ($record = mysql_fetch_object($res)) {
			$result[$record->name] = $record->id;
		}
		return $result;
	}
	
	/**
	 * Vrati pole ID temat (topics), ve kterych jsou ulozeny literarni souteze.
	 * @return mixed
	 */
	public static function getTopicsID() {
		return array(
			self::ID_CATEGORY_1_TOPIC,
			self::ID_CATEGORY_2_TOPIC,
			self::ID_CATEGORY_3_TOPIC
		);
	}
	
	
}
?>